package org.jsrml.sv.mob.controller.supplier;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.jsrml.common.pagination.Pagination;
import org.jsrml.common.util.JSONResultDTO;
import org.jsrml.core.supplier.command.DoVendorStatusCo;
import org.jsrml.core.supplier.command.RemoveVendorCo;
import org.jsrml.core.supplier.command.SaveSupplyBankCo;
import org.jsrml.core.supplier.command.SaveVendorCo;
import org.jsrml.core.supplier.entity.SupplyBank;
import org.jsrml.core.supplier.entity.Vendor;
import org.jsrml.core.supplier.exception.SupplierException;
import org.jsrml.core.supplier.qo.SupplyBankQO;
import org.jsrml.core.supplier.qo.VendorQO;
import org.jsrml.core.supplier.service.SupplyBankService;
import org.jsrml.core.supplier.service.VendorService;
import org.jsrml.sv.mob.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/vendor")
public class VendorController extends BaseController {
	
	@Autowired
	private VendorService vendorService;

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO create(@RequestBody SaveVendorCo command) {
		Vendor vendor = null;
		try {
			vendor = vendorService.create(command);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(vendor);
	}

	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO modify(@RequestBody SaveVendorCo command) {
		Vendor vendor =null;
		try {
			vendor = vendorService.modify(command);
		} catch (SupplierException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(vendor);
	}

	@RequestMapping(value = "/pl/remove", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO plRemove(@RequestBody RemoveVendorCo command) {
		try {
			vendorService.plRemove(command);
		} catch (SupplierException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}

	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO remove(@RequestBody RemoveVendorCo command) {
		try {
			vendorService.remove(command);
		} catch (SupplierException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}

	@RequestMapping("/query")
	@ResponseBody
	public JSONResultDTO query(HttpServletRequest request, @RequestBody VendorQO qo) {
		qo.setRemove(false);
		try {
			switch (qo.getResultType()) {
			case 0:
				Integer count = vendorService.queryCount(qo);
				return createSuccessResult(count);
			case 1:
				Vendor vendor = vendorService.queryUnique(qo);
				return createSuccessResult(vendor);
			case 2:
				List<Vendor> list = vendorService.queryList(qo);
				return createSuccessResult(list);
			case 3:
				Pagination pagination = vendorService.queryPagination(qo);
				return createSuccessResult(pagination);
			default:
				return createFailResult("-1", "参数中缺少正确的resultType", null);
			}
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
	}
	
	/**
	 * 修改供应商状态
	 * @param command
	 * @return
	 */
	@RequestMapping(value = "/doVendorStatus", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO doVendorStatus(@RequestBody DoVendorStatusCo command) {
		try {
			vendorService.doVendorStatus(command);
		} catch (SupplierException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}
	
	/**
	 * 供应商银行信息
	 */
	@Autowired
	private SupplyBankService supplyBankService;
	
	@RequestMapping(value = "/bank/create", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO bankCreate(@RequestBody SaveSupplyBankCo command) {
		SupplyBank supplyBank = null;
		try {
			supplyBank = supplyBankService.create(command);
		}catch(SupplierException e){
			return createFailResult("-1", e.getMessage(), null);
		}catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		
		return createSuccessResult(supplyBank);
	}
	
	@RequestMapping(value = "/bank/modify", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO bankModify(@RequestBody SaveSupplyBankCo command) {
		SupplyBank supplyBank = null;
		try {
			supplyBank = supplyBankService.modify(command);
		}catch(SupplierException e){
			return createFailResult("-1", e.getMessage(), null);
		}catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		
		return createSuccessResult(supplyBank);
	}
	
	@RequestMapping("/bank/close/{bankId}")
	@ResponseBody
	public JSONResultDTO bankClose(@PathVariable String bankId) {
		try {
			supplyBankService.close(bankId);
		} catch (SupplierException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}
	
	@RequestMapping("/bank/query")
	@ResponseBody
	public JSONResultDTO bankQuery(HttpServletRequest request, @RequestBody SupplyBankQO qo) {
		try {
			switch (qo.getResultType()) {
			case 0:
				Integer count = supplyBankService.queryCount(qo);
				return createSuccessResult(count);
			case 1:
				SupplyBank supplyBank = supplyBankService.queryUnique(qo);
				return createSuccessResult(supplyBank);
			case 2:
				List<SupplyBank> list = supplyBankService.queryList(qo);
				return createSuccessResult(list);
			case 3:
				Pagination pagination = supplyBankService.queryPagination(qo);
				return createSuccessResult(pagination);
			default:
				return createFailResult("-1", "参数中缺少正确的resultType", null);
			}
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
	}
	
}
